package lt.egidijus.aatest.model;

import java.util.ArrayList;

import lt.egidijus.aatest.junit.JUnitEngine;
import lt.egidijus.aatest.utils.Constants;
import lt.egidijus.aatest.utils.Utils;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IType;

public class AAtestList {
	
	private ArrayList<AAtestClass> list = new ArrayList<AAtestClass>();
	private ArrayList<ICompilationUnit> testSuiteList = new ArrayList<ICompilationUnit>();
	
	public void add(ICompilationUnit unit){
		String elementName = unit.getElementName();
		IType type = unit.findPrimaryType();
		if (JUnitEngine.isJUnitTestSuite(type)){
			testSuiteList.add(unit);
		} else if (elementName.lastIndexOf(Constants.testClassPostfix) > 0 || elementName.endsWith(".java")){
			addClass(unit);
		}
	}
	
	public void addList(ArrayList<ICompilationUnit> cUnitList){
		for (ICompilationUnit cUnit: cUnitList){
			add(cUnit);
		}
	}
	
	private void addClass(ICompilationUnit cUnit){
		AAtestClass aatClass = getAATestClass(cUnit);
		if (aatClass == null){
			aatClass = new AAtestClass(cUnit);
			list.add(aatClass);
		}
		else aatClass.add(cUnit);
		
	}
	
	private AAtestClass getAATestClass(ICompilationUnit cUnit){	
		return getAATestClass(Utils.getClassName(cUnit));
	}
	
	public AAtestClass getAATestClass(String name){
		for (AAtestClass tClass : list){
			if (tClass.getName().equals(name))
				return tClass;
		}
		return null;
	}

	public ArrayList<AAtestClass> getList() {
		return list;
	}

	public void setList(ArrayList<AAtestClass> list) {
		this.list = list;
	}

	public ArrayList<ICompilationUnit> getTestSuiteList() {
		return testSuiteList;
	}

	public void setTestSuiteList(ArrayList<ICompilationUnit> testSuiteList) {
		this.testSuiteList = testSuiteList;
	}
	
	

}
